package com.cherrypt.api.core;

import com.cherrypt.api.core.service.ConfigService;
import com.cherrypt.api.core.service.impl.CaptchaStorageService;
import com.cherrypt.api.core.util.CherryUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;

@EnableCaching
@SpringBootApplication
public class ApiApplication {
    private static final Logger logger = LoggerFactory.getLogger(ApiApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
        logger.info("{}", CherryUtils.config);
    }

    @Bean
    CommandLineRunner init(ConfigService configService,
                           CaptchaStorageService captchaStorageService) {
        return (args -> {
            // 先删除再缓存
            configService.removeRedisConfig();
            CherryUtils.config = configService.getConfig();
            logger.info("正在清空验证码图片目录, 结果: {}", captchaStorageService.deleteAll());
            captchaStorageService.init();
        });
    }
}
